<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"inkgn.gitee.io","root":"/","scheme":"Muse","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="vue引入全局css当我们使用vue cli 建立一个项目后，通常会使用sass-loader 来编写我们的css样式，它有一个很实用的特性，在某一组件下的css代码 最终只会作用于该组件上，这种方式避免了组件样式的交叉污染，但是对于一些页面通用样式就显得十分不友好了，比如一些HTML标签默认存在的margin，padding，我们通常希望它们在页面初始化时就以none的样式出现，这个时候就需要我">
<meta property="og:type" content="article">
<meta property="og:title" content="Vue中引入全局sass">
<meta property="og:url" content="https://inkgn.gitee.io/2020/11/28/list2/index.html">
<meta property="og:site_name" content="静水深流">
<meta property="og:description" content="vue引入全局css当我们使用vue cli 建立一个项目后，通常会使用sass-loader 来编写我们的css样式，它有一个很实用的特性，在某一组件下的css代码 最终只会作用于该组件上，这种方式避免了组件样式的交叉污染，但是对于一些页面通用样式就显得十分不友好了，比如一些HTML标签默认存在的margin，padding，我们通常希望它们在页面初始化时就以none的样式出现，这个时候就需要我">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-11-28T14:04:05.000Z">
<meta property="article:modified_time" content="2020-11-28T14:04:05.000Z">
<meta property="article:author" content="yinks">
<meta property="article:tag" content="vue">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://inkgn.gitee.io/2020/11/28/list2/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>Vue中引入全局sass | 静水深流</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">静水深流</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/categories" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
        <li class="menu-item menu-item-motto">

    <a href="/motto/motto" rel="section"><i class="fa fa-book fa-fw"></i>古训</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/me" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://inkgn.gitee.io/2020/11/28/list2/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/user_info/face">
      <meta itemprop="name" content="yinks">
      <meta itemprop="description" content="长路漫漫，半程风雨，半程春">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="静水深流">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Vue中引入全局sass
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-11-28 22:04:05" itemprop="dateCreated datePublished" datetime="2020-11-28T22:04:05+08:00">2020-11-28</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/vue/" itemprop="url" rel="index"><span itemprop="name">vue</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h3 id="vue引入全局css"><a href="#vue引入全局css" class="headerlink" title="vue引入全局css"></a>vue引入全局css</h3><p>当我们使用vue cli 建立一个项目后，通常会使用sass-loader 来编写我们的css样式，它有一个很实用的特性，在某一组件下的css代码 最终只会作用于该组件上，这种方式避免了组件样式的交叉污染，但是对于一些页面通用样式就显得十分不友好了，比如一些HTML标签默认存在的margin，padding，我们通常希望它们在页面初始化时就以none的样式出现，这个时候就需要我们使用vue当中的全局引入了，方法也非常的简单</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"> </span><br><span class="line"><span class="comment">// 文件结构</span></span><br><span class="line">style/</span><br><span class="line">    font.scss</span><br><span class="line">    index.scss</span><br><span class="line">    main.scss</span><br><span class="line"></span><br><span class="line"><span class="comment">// 主文件内容</span></span><br><span class="line">index.scss</span><br><span class="line">@<span class="keyword">import</span> <span class="string">&quot;./main.scss&quot;</span>;</span><br><span class="line">@<span class="keyword">import</span> <span class="string">&quot;./font.scss&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 全局引入</span></span><br><span class="line">main.js</span><br><span class="line"><span class="keyword">import</span> <span class="string">&quot;@/style/index.scss&quot;</span>;    <span class="comment">// @代表默认src</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>我们一般的css全局引入就是这样，但是当你的文件中是以sass的写法完成时你会发现这种方法就会失效，并且报错，你可能注意到我上面文件结构中也是引入的sass为什么会成功呢？</p>
<p>那是因为我虽然文件是以sass做后缀，但其中的内容还是规范的css写法，所以这种引入依然是生效的，但如果你的文件中使用了sass变量或者嵌套语法，那vue一定会给你抛出一个error，</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"> </span><br><span class="line">//sass写法</span><br><span class="line">$theme-color: #C62F2F;</span><br><span class="line">$black: #000;</span><br><span class="line">$white: #fff;</span><br><span class="line">$gold: #e7aa5a;</span><br><span class="line">$blue: #517eaf;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> span&#123;</span><br><span class="line">    position: absolute;</span><br><span class="line">    .iconfont&#123;</span><br><span class="line">      font-size: 15px;</span><br><span class="line">      color: rgba($color: #eec0c0, $alpha: .8);</span><br><span class="line">    &#125;</span><br><span class="line">｝</span><br><span class="line">    </span><br></pre></td></tr></table></figure>

<p>经过一番查询后发现，针对sass全局引入的方法也是有的，首先我们在vue项目目录中创建<code>vue.config.js</code>文件，这是一个可选文件，<br>在vue2.x时期通过脚手架创建的vue项目是自带这个文件的，到vue3.x时期vue取消了这个文件，当开发者需要时可以自行创建，创建的位置是项目的顶级目录（package.json同级）创建后我们写入内容：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"> </span><br><span class="line"> <span class="built_in">module</span>.exports = &#123;</span><br><span class="line">    <span class="attr">css</span>: &#123;</span><br><span class="line">        <span class="attr">loaderOptions</span>: &#123;</span><br><span class="line">            <span class="attr">sass</span>: &#123;</span><br><span class="line">                <span class="attr">Data</span>: <span class="string">`</span></span><br><span class="line"><span class="string">                @import &quot;@/style/variables.scss&quot;;</span></span><br><span class="line"><span class="string">                @import &quot;@/style/iconfont.scss&quot;;</span></span><br><span class="line"><span class="string">                `</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>以上写法是webpack提供的标准格式，值得注意的是，这种写法并不能回回奏效，如果碰巧你运气跟我一样糟糕，不要怕，根据vue 提示的报错信息进行调试，vue报错中会返回一个数组，挨个尝试，把data替换掉，我最终把data换成了prependData程序终于正常跑起来了</p>
<p>2021 9.3 更新</p>
<blockquote>
<p>仅仅是sass新旧版本的差别。如果你正在使用旧版，那么就使用data，如果使用新版就用prependData</p>
</blockquote>

    </div>

    
    
    
        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>yinks
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://inkgn.gitee.io/2020/11/28/list2/" title="Vue中引入全局sass">https://inkgn.gitee.io/2020/11/28/list2/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/vue/" rel="tag"># vue</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2020/11/23/vue-pro3/" rel="prev" title="vue项目练习笔记">
      <i class="fa fa-chevron-left"></i> vue项目练习笔记
    </a></div>
      <div class="post-nav-item">
    <a href="/2020/11/29/list3/" rel="next" title="阿里图标库iconfont使用问题笔记">
      阿里图标库iconfont使用问题笔记 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#vue%E5%BC%95%E5%85%A5%E5%85%A8%E5%B1%80css"><span class="nav-number">1.</span> <span class="nav-text">vue引入全局css</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="yinks"
      src="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/user_info/face">
  <p class="site-author-name" itemprop="name">yinks</p>
  <div class="site-description" itemprop="description">长路漫漫，半程风雨，半程春</div>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://gitee.com/inkgn" title="Gitee → https:&#x2F;&#x2F;gitee.com&#x2F;inkgn" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>Gitee</a>
      </span>
      <span class="links-of-author-item">
        <a href="/2087104694@qq.com" title="E-Mail → 2087104694@qq.com"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">yinks</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://muse.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

</body>
</html>
